﻿using System.IO;
using System.Text;
using ExcelDataReader;
using GamePlayConfig;
using UnityEditor;
using UnityEngine;

namespace Editor
{
    public static class TestMenu
    {
        [MenuItem("Test/Test", false, 1)]
        public static void Test()
        {

        }


        [MenuItem("Test/TestList")]
        public static void  TestList()
        {
            using var stream = File.Open(Path.Combine(Application.dataPath, "GamePlayConfig/Output/Monster.bin"), FileMode.Open, FileAccess.Read);
            using var reader = new BinaryReader(stream);

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                var monsterCfg = new GamePlayConfig.MonsterCfg();
                monsterCfg.Deserialize(reader);
                Debug.Log($"ID:{monsterCfg.ID} Name:{monsterCfg.Name} Age:{monsterCfg.Age} Skill:{monsterCfg.Skill} Array1:{string.Join(",", monsterCfg.Array1)} ");
            }
        }

        [MenuItem("Test/TestLoadCfg")]
        public static void TestLoadCfg()
        {
            var keys = MonsterCfg.Keys();
            
            foreach (var key in keys)
            {
                Debug.Log($"ID:{key}");
            }
        }
        
        [MenuItem("Test/TestReadHd")]
        public static void TestReadHd()
        {
            using var hdStream = File.Open(Path.Combine(Application.dataPath, "GamePlayConfig/Output/Monster_hd.bin"), FileMode.Open, FileAccess.Read);
            using var hdReader = new BinaryReader(hdStream);
            
            using var binStream = File.Open(Path.Combine(Application.dataPath, "GamePlayConfig/Output/Monster.bin"), FileMode.Open, FileAccess.Read);
            using var binReader = new BinaryReader(binStream);

            
            int lookupId = 3;
            while (hdReader.BaseStream.Position < hdReader.BaseStream.Length)
            {
                var id = hdReader.ReadInt32();
                var offset = hdReader.ReadInt64();
                if (id == lookupId)
                {
                    binReader.BaseStream.Seek(offset, SeekOrigin.Begin);
                    var monsterCfg = new GamePlayConfig.MonsterCfg();
                    monsterCfg.Deserialize(binReader);
                    Debug.Log($"ID:{monsterCfg.ID} Name:{monsterCfg.Name} Age:{monsterCfg.Age} Skill:{monsterCfg.Skill} Array1:{string.Join(",", monsterCfg.Array1)} ");
                    break;
                }
            }
            
            hdStream.Close();
            binStream.Close();

            MonsterCfg cfg = MonsterCfg.Get(3);
            Debug.Log($"ID:{cfg.ID} Name:{cfg.Name} Age:{cfg.Age} Skill:{cfg.Skill} Array1:{string.Join(",", cfg.Array1)} ");

            var keys = MonsterCfg.Keys();
            foreach (var key in keys)
            {
                Debug.Log($"ID:{key}");
            }
        }
    }
}